/**
 * 奇门遁甲局数查询组件
 * 根据度数判断对应的山向、局数和时数
 */
class QiMenJuShuQuery {
  constructor() {
    this.data = this._initializeData();
  }

  // 初始化数据
  _initializeData() {
    return [
      {
        shanxiang: "子山午向",
        juShu: ["阳二局", "阳八局", "阳五局"],
        degreeRanges: [
          { min: 172.5, max: 177.4 },
          { min: 177.5, max: 182.4 },
          { min: 182.5, max: 187.4 },
        ],
        shi: 12,
      },
      {
        shanxiang: "癸山丁向",
        juShu: ["阳三局", "阳九局", "阳六局"],
        degreeRanges: [
          { min: 187.5, max: 192.4 },
          { min: 192.5, max: 197.4 },
          { min: 197.5, max: 202.4 },
        ],
        shi: 14,
      },
      {
        shanxiang: "丑山未向",
        juShu: ["阳八局", "阳五局", "阳二局"],
        degreeRanges: [
          { min: 202.5, max: 207.4 },
          { min: 207.5, max: 212.4 },
          { min: 212.5, max: 217.4 },
        ],
        shi: 14,
      },
      {
        shanxiang: "艮山坤向",
        juShu: ["阳九局", "阳六局", "阳三局"],
        degreeRanges: [
          { min: 217.5, max: 222.4 },
          { min: 222.5, max: 227.4 },
          { min: 227.5, max: 232.4 },
        ],
        shi: 16,
      },
      {
        shanxiang: "寅山申向",
        juShu: ["阳一局", "阳七局", "阳四局"],
        degreeRanges: [
          { min: 232.5, max: 237.4 },
          { min: 237.5, max: 242.4 },
          { min: 242.5, max: 247.4 },
        ],
        shi: 16,
      },
      {
        shanxiang: "甲山庚向",
        juShu: ["阳三局", "阳九局", "阳六局"],
        degreeRanges: [
          { min: 247.5, max: 252.4 },
          { min: 252.5, max: 257.4 },
          { min: 257.5, max: 262.4 },
        ],
        shi: 18,
      },
      {
        shanxiang: "卯山酉向",
        juShu: ["阳四局", "阳一局", "阳七局"],
        degreeRanges: [
          { min: 262.5, max: 267.4 },
          { min: 267.5, max: 272.4 },
          { min: 272.5, max: 277.4 },
        ],
        shi: 18,
      },
      {
        shanxiang: "乙山辛向",
        juShu: ["阳五局", "阳二局", "阳八局"],
        degreeRanges: [
          { min: 277.5, max: 282.4 },
          { min: 282.5, max: 287.4 },
          { min: 287.5, max: 292.4 },
        ],
        shi: 20,
      },
      {
        shanxiang: "辰山戌向",
        juShu: ["阳四局", "阳一局", "阳七局"],
        degreeRanges: [
          { min: 292.5, max: 297.4 },
          { min: 297.5, max: 302.4 },
          { min: 302.5, max: 307.4 },
        ],
        shi: 20,
      },
      {
        shanxiang: "巽山乾向",
        juShu: ["阳五局", "阳二局", "阳八局"],
        degreeRanges: [
          { min: 307.5, max: 312.4 },
          { min: 312.5, max: 317.4 },
          { min: 317.5, max: 322.4 },
        ],
        shi: 22,
      },
      {
        shanxiang: "巳山亥向",
        juShu: ["阴六局", "阴三局", "阴九局"],
        degreeRanges: [
          { min: 322.5, max: 327.4 },
          { min: 327.5, max: 332.4 },
          { min: 332.5, max: 337.4 },
        ],
        shi: 22,
      },
      {
        shanxiang: "丙山壬向",
        juShu: ["阴九局", "阴三局", "阴六局"],
        degreeRanges: [
          { min: 337.5, max: 342.4 },
          { min: 342.5, max: 347.4 },
          { min: 347.5, max: 352.4 },
        ],
        shi: 0,
      },
      {
        shanxiang: "午山子向",
        juShu: ["阴八局", "阴二局", "阴五局"],
        degreeRanges: [
          { min: 352.5, max: 357.4 },
          { min: 357.5, max: 2.4 },
          { min: 2.5, max: 7.4 },
        ],
        shi: 0,
      },
      {
        shanxiang: "丁山癸向",
        juShu: ["阴七局", "阴一局", "阴四局"],
        degreeRanges: [
          { min: 7.5, max: 12.4 },
          { min: 12.5, max: 17.4 },
          { min: 17.5, max: 22.4 },
        ],
        shi: 2,
      },
      {
        shanxiang: "未山丑向",
        juShu: ["阴二局", "阴五局", "阴八局"],
        degreeRanges: [
          { min: 22.5, max: 27.4 },
          { min: 27.5, max: 32.4 },
          { min: 32.5, max: 37.4 },
        ],
        shi: 2,
      },
      {
        shanxiang: "坤山艮向",
        juShu: ["阴一局", "阴七局", "阴四局"],
        degreeRanges: [
          { min: 37.5, max: 42.4 },
          { min: 42.5, max: 47.4 },
          { min: 47.5, max: 52.4 },
        ],
        shi: 4,
      },
      {
        shanxiang: "申山寅向",
        juShu: ["阴九局", "阴三局", "阴六局"],
        degreeRanges: [
          { min: 52.5, max: 57.4 },
          { min: 57.5, max: 62.4 },
          { min: 62.5, max: 67.4 },
        ],
        shi: 4,
      },
      {
        shanxiang: "庚山甲向",
        juShu: ["阴七局", "阴一局", "阴四局"],
        degreeRanges: [
          { min: 67.5, max: 72.4 },
          { min: 72.5, max: 77.4 },
          { min: 77.5, max: 82.4 },
        ],
        shi: 6,
      },
      {
        shanxiang: "酉山卯向",
        juShu: ["阴六局", "阴九局", "阴三局"],
        degreeRanges: [
          { min: 82.5, max: 87.4 },
          { min: 87.5, max: 92.4 },
          { min: 92.5, max: 97.4 },
        ],
        shi: 6,
      },
      {
        shanxiang: "辛山乙向",
        juShu: ["阴五局", "阴二局", "阴八局"],
        degreeRanges: [
          { min: 97.5, max: 102.4 },
          { min: 102.5, max: 107.4 },
          { min: 107.5, max: 112.4 },
        ],
        shi: 8,
      },
      {
        shanxiang: "戌山辰向",
        juShu: ["阴六局", "阴九局", "阴三局"],
        degreeRanges: [
          { min: 112.5, max: 117.4 },
          { min: 117.5, max: 122.4 },
          { min: 122.5, max: 127.4 },
        ],
        shi: 8,
      },
      {
        shanxiang: "乾山巽向",
        juShu: ["阴五局", "阴八局", "阴二局"],
        degreeRanges: [
          { min: 127.5, max: 132.4 },
          { min: 132.5, max: 137.4 },
          { min: 137.5, max: 142.4 },
        ],
        shi: 10,
      },
      {
        shanxiang: "亥山巳向",
        juShu: ["阳四局", "阳一局", "阳七局"],
        degreeRanges: [
          { min: 142.5, max: 147.4 },
          { min: 147.5, max: 152.4 },
          { min: 152.5, max: 157.4 },
        ],
        shi: 10,
      },
      {
        shanxiang: "壬山丙向",
        juShu: ["阳一局", "阳四局", "阳七局"],
        degreeRanges: [
          { min: 157.5, max: 162.4 },
          { min: 162.5, max: 167.4 },
          { min: 167.5, max: 172.4 },
        ],
        shi: 12,
      },
    ];
  }

  // 判断度数是否在范围内（处理360度循环）
  _isInRange(d, min, max) {
    if (min <= max) {
      return d >= min && d <= max;
    } else {
      return d >= min || d <= max;
    }
  }

  /**
   * 根据度数查询奇门遁甲局数信息
   * @param {number} degree - 输入度数
   * @returns {Object} 返回包含山向、局数和时数的对象
   */
  query(degree) {
    const d = ((degree % 360) + 360) % 360; // 处理负数和超过360的情况

    for (const item of this.data) {
      for (let i = 0; i < 3; i++) {
        const range = item.degreeRanges[i];
        if (this._isInRange(d, range.min, range.max)) {
          return {
            shanxiang: item.shanxiang,
            juShu: item.juShu[i],
            shi: item.shi,
            degree: `${range.min}-${range.max}`,
          };
        }
      }
    }

    // 理论上不会执行到这里，但为了完整性
    return {
      shanxiang: "未知",
      juShu: "未知",
      shi: 0,
      degree: d,
    };
  }
}

// 导出组件
export default QiMenJuShuQuery;

// 浏览器环境使用示例
// const qiMenQuery = new QiMenJuShuQuery();
// const result = qiMenQuery.query(175);
// console.log(result);
